package com.kedacom.dkvideoplayer;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.dueeeke.videoplayer.exo.ExoMediaPlayerFactory;
import com.dueeeke.videoplayer.player.VideoView;

import java.io.File;

/**
 * 全屏播放
 * Created by Devlin_n on 2017/4/21.
 * <p>
 * https://github.com/Doikki/DKVideoPlayer/wiki#%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8
 * <p>
 * 说明：
 * 1、基于v3.0.3版本，因为后续的版本使用的是AndroidX；
 * 2、v3.0.3中全屏播放时有bug，左上角返回、重播按钮无效；目前本人已修复；
 */

public class FullScreenActivity extends AppCompatActivity {

    private FullScreenVideoView mVideoView;

    /**
     * 播放本地视频
     *
     * @param activity
     * @param localPath
     * @param title
     */
    public static void playLocalVideo(Activity activity, String localPath, String title) {
        Intent mIntent = new Intent(activity, FullScreenActivity.class);
        mIntent.putExtra("localPath", localPath);
        mIntent.putExtra("title", title);
        activity.startActivity(mIntent);
    }

    /**
     * 播放网络视频
     *
     * @param activity
     * @param videoUrl
     * @param title
     */
    public static void playOnlineVideo(Activity activity, String videoUrl, String title) {
        Intent mIntent = new Intent(activity, FullScreenActivity.class);
        mIntent.putExtra("videoUrl", videoUrl);
        mIntent.putExtra("title", title);
        activity.startActivity(mIntent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String titleStr = getIntent().getStringExtra("title");
        String localPath = getIntent().getStringExtra("localPath");//本地视频路径
        String videoUrl = getIntent().getStringExtra("videoUrl");//网络视频路径

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle("标题名称");
            actionBar.setDisplayHomeAsUpEnabled(true);
        } else {
            Log.d("caowj", "actionBar is null");
        }

        mVideoView = new FullScreenVideoView(this);
        setContentView(mVideoView);
//        mVideoView.setUrl("http://vfx.mtime.cn/Video/2019/03/12/mp4/190312143927981075.mp4");
//        mVideoView.setUrl("http://192.168.20.180/pacc/attachment/v1/paccFileDownload?filePath=2021-08-16/210816092644210810094303VID_20210810_093643.mp4");
//        String path = "/storage/emulated/0/Quark/Download/210810094303VID_20210810_093643.mp4";

        if (TextUtils.isEmpty(localPath) && TextUtils.isEmpty(videoUrl)) {
            Toast.makeText(getBaseContext(), "缺少视频地址", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        if (!TextUtils.isEmpty(localPath)) {
            File file = new File(localPath);
            if (file.exists()) {
                localPath = Uri.parse("file://" + file.getAbsolutePath()).toString();
            }
            mVideoView.setUrl(localPath);
        }

        if (!TextUtils.isEmpty(videoUrl)) {
            mVideoView.setUrl(videoUrl);
        }

        FullScreenController controller = new FullScreenController(this);
        controller.setTitle("这是一个标题2");
        mVideoView.setVideoController(controller);
        //使用ExoPlayer解码
        mVideoView.setPlayerFactory(ExoMediaPlayerFactory.create(this));
//        mVideoView.setScreenScale(VideoView.SCREEN_SCALE_16_9);
        mVideoView.setScreenScale(VideoView.SCREEN_SCALE_DEFAULT);
        mVideoView.start();

    }

    @Override
    protected void onPause() {
        super.onPause();
        mVideoView.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mVideoView.resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVideoView.release();
    }

    @Override
    public void onBackPressed() {
        if (!mVideoView.onBackPressed()) {
            super.onBackPressed();
        }
    }
}
